/* eslint-disable no-unused-vars */
<!--
 * @Author: yuSun
 * @Date: 2022-02-10 11:51:06
 * @LastEditTime: 2022-03-28 10:29:25
 * @LastEditors: Please set LastEditors
 * @Description: 轮播图公用样式,封装组件
-->
<template>
  <div class='xtx-carousel' @mouseenter="stop" @mouseleave="start">
    <!-- 轮播图图片容器 -->
    <ul class="carousel-body">
      <li class="carousel-item" :class="{ fade:index===ind }" v-for="(item,index) in bannerList" :key="index" >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <!-- 左右两侧的家箭头 -->
    <a href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left" @click="toggle(-1)"></i></a>
    <a href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right" @click="toggle(1)"></i></a>
    <!-- 底部的小白点 -->
    <div class="carousel-indicator">
      <span @click="ind=index" v-for="(item,index) in bannerList" :key="index"  :class="{active:index===ind}"></span>
    </div>
  </div>
</template>

<script>
import { onMounted, ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 轮播图数据列表
    bannerList: {
      type: Array,
      default: () => []
    },
    // 是否轮播
    autoPlay: {
      type: Boolean,
      default: false
    },
    // 多久切换一次
    changeNum: {
      type: Number,
      default: 3000
    }
  },
  setup (props) {
    // 定义默认显示那一张图
    const ind = ref(0)

    //  需要用定时器来做轮播图
    // eslint-disable-next-line no-unused-vars
    let timer = null
    const playFn = () => {
      clearInterval(timer)
      timer = setInterval(() => {
        ind.value++
        if (ind.value >= props.bannerList.length) {
          // 重置
          ind.value = 0
        }
      }, props.changeNum)
    }
    // 什么时候开始轮播需要监听数据的变化,没有数据就不要轮播了,所以监听数组的长度, 播到最后一张就要重复一遍
    watch(() => props.bannerList, (val) => {
      if (val.length && props.autoPlay) {
        playFn()
      }
    }, { immediate: true })
    // 鼠标经过暂停 离开开始
    const stop = () => {
      if (timer) {
        clearInterval(timer)
      }
    }
    const start = () => {
      if (props.bannerList.length && props.autoPlay) {
        playFn()
      }
    }
    const toggle = (val) => {
      // 接收一下将要改变的索引
      const newV = ind.value + val
      // 判断大小 改变的索引大于数据的索引 重置为0
      if (newV > props.bannerList.length - 1) {
        ind.value = 0
        return
      }
      // 改变的所以小于0 重置为数据的索引
      if (newV < 0) {
        ind.value = props.bannerList.length - 1
        return
      }
      // 正常情况
      ind.value = newV
    }
    // 卸载组件
    onMounted(() => {
      clearInterval(timer)
    })
    return {
      ind,
      stop,
      start,
      toggle
    }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel{
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel{
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0,0,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,0,0,.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev{
        left: 20px;
      }
      &.next{
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
